package com.sx.micowell.framework.adapter.binding;


import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.databinding.BindingAdapter;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.mvvm.until.GlideApp;
import com.example.mvvm.until.GlideRequest;

public class ImageViewBinding {


    @BindingAdapter(value = {"glideUrl", "glideRound", "glideCenter", "glideRadius", "glideId", "emptyDrawable", "errorImage", "noCache"}, requireAll = false)
    public static void glideImage(ImageView view, String url, boolean round, boolean center, int radius, @DrawableRes int drawableId, Drawable defaultImage, Drawable errorImage, boolean noCache) {
        if (view != null) {
            GlideRequest load = GlideApp.with(view.getContext()).asBitmap();
            if (noCache) {
                load.skipMemoryCache(true) // 不使用内存缓存
                        .diskCacheStrategy(DiskCacheStrategy.NONE); // 不使用磁盘缓存
            }
            if (errorImage != null) {
                load.error(errorImage);
            }
            if (round) {
                load.circleCrop();
            }
            if (center) {
                load.centerCrop();
            }
            if (radius != 0) {
                load.apply(RequestOptions.bitmapTransform(new RoundedCorners(radius)));
            }
            if (!TextUtils.isEmpty(url)) {
                load.load(url);
                load.into(view);
            } else if (drawableId != 0) {
                load.load(drawableId);
                load.into(view);
            }
            if (TextUtils.isEmpty(url) && drawableId == 0 && defaultImage != null) {
                view.setImageDrawable(defaultImage);
            }


        }
    }

/*    @BindingAdapter(value = {"zxingCode", "zxingWidth", "zxingHeight"})
    public static void bindZxing(ImageView view, String code, int width, int height) {
        if (view != null && !TextUtils.isEmpty(code)) {
            Bitmap bitmap = HWImageManager.getInstance().getBitmapFun().imageErCode(code, view.getContext(), DisplayUtil.dp2px(view.getContext(), width), DisplayUtil.dp2px(view.getContext(), height));
            if (bitmap != null) {
                GlideApp.with(view.getContext()).load(bitmap).into(view);
            }

        }
    }*/

    /*加载长图*//*
    @BindingAdapter(value = "glideLong")
    public static void bingLongImageView(BigView imageView, String imageUrl) {
        if (imageView != null) {
            OkGo.<File>get("https://dghjy.timedg.com:8801/good-education-manage/file/32/CE969BAFC458C78A471CCD66D92D60FA.png")
                    .execute(new FileCallback() {
                        @Override
                        public void onSuccess(Response<File> response) {
                            try {
                                FileInputStream fileInputStream = new FileInputStream(response.body());
                                imageView.setImage(fileInputStream);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

    }*/


}
